//
//  SLDownloadViewModel.m
//  Objective-C
//
//  Created by S_LINE on 2019/9/9.
//  Copyright © 2019 S_Line. All rights reserved.
//

#import "SLDownloadViewModel.h"

#import <YYKit/YYKit.h>

#import "SLDownloadFileModel.h"
#import "SLDownloadDataManager.h"
#import "SLDownloadTaskManager.h"
#import "SLDownloadMissionManager.h"

@interface SLDownloadViewModel ()

@property (nonatomic, strong) SLDownloadFileModel *notifyFileModel;
@property (nonatomic, strong) NSArray *dataList;

@end

@implementation SLDownloadViewModel

- (instancetype)init {
    self = [super init];
    if (self) {
        [self initDatas];
        [self initDataList];
        [self addNotifications];
    }
    return self;
}

- (void)initDatas {
    NSArray *fileModelsInDataBase = [[SLDownloadDataManager shareInstance] queryAllDatas];
    NSArray *fileModelsInFile = [self getTargetDownloadDatas];
    
    for (int i = 0; i < fileModelsInFile.count; i++) {
        SLDownloadFileModel *fileModelInFile = [fileModelsInFile objectAtIndex:i];
        
        BOOL fileModelHasInDataBase = NO;
        for (SLDownloadFileModel *fileModelInDatabase in fileModelsInDataBase) {
            if ([fileModelInDatabase isKindOfClass:[SLDownloadFileModel class]] && [fileModelInDatabase.fileId isEqualToString:fileModelInFile.fileId]) {
                fileModelHasInDataBase = YES;
            }
        }
        
        if (!fileModelHasInDataBase) {
            [fileModelInFile saveData];
        }
    }
}

- (void)initDataList {
    self.dataList = [[SLDownloadMissionManager shareInstance] queryAllData];
}

- (void)addNotifications {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(downloadStateChangeNoti:) name:@"SLDownloadFileModelDownloadStateChanged" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(downloadProgressChangeNoti:) name:@"SLDownloadFileModelDownloadProgressChanged" object:nil];
}

- (void)downloadStateChangeNoti:(NSNotification *)noti {
    self.notifyFileModel = noti.object;
}

- (void)downloadProgressChangeNoti:(NSNotification *)noti {
    self.notifyFileModel = noti.object;
}

#pragma mark - setter & getter
- (void)setNotifyFileModel:(SLDownloadFileModel *)notifyFileModel {
    _notifyFileModel = notifyFileModel;
    
    [self.dataList enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        SLDownloadFileModel *fileModel = obj;
        
        if ([fileModel.fileId isEqualToString:notifyFileModel.fileId]) {
            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:idx inSection:0];
            
            if (self.reloadAction) {
                self.reloadAction(indexPath);
            }
            
            *stop = YES;
        }
    }];
}

/**
 * 获取本地文件中可供下载的文件列表
 */
- (NSArray *)getTargetDownloadDatas {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"DownloadFiles" ofType:@"plist"];
    NSArray *array = [NSArray arrayWithContentsOfFile:path];
    
    NSMutableArray *fileModelArray = [NSMutableArray new];
    for (int i = 0; i < array.count; i++) {
        NSDictionary *dict = [array objectAtIndex:i];
        
        if ([dict isKindOfClass:[NSDictionary class]]) {
            SLDownloadFileModel *fileModel = [SLDownloadFileModel modelWithDictionary:dict];
            
            if ([fileModel isKindOfClass:[SLDownloadFileModel class]]) {
                [fileModelArray addObject:fileModel];
            }
        }
    }
    
    return fileModelArray;
}

#pragma mark - DataSource
- (NSInteger)numberOfSections {
    return 1;
}

- (NSInteger)numberOfRowsInSection:(NSInteger)section {
    return self.dataList.count;
}

- (id)dataForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row < self.dataList.count) {
        return [self.dataList objectAtIndex:indexPath.row];
    }
    
    return nil;
}

@end
